package com.cs.pk.codestriker;

import org.eclipse.swt.widgets.Combo;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.cs.pk.Activator;
import com.cs.pk.utils.Constants;
import com.cs.pk.utils.UtilRepository;

public class Projects extends CodeStriker{
	
	private String projectAction = null;
	private String projectTagName = null;
	private String projectAttribute = null;
	private Combo projectCombo = null;
	public Projects(Object obj) {
		super(obj);
		projectCombo = (Combo)obj;
		projectAction = Activator.getDefault().getDefaultPreference(Constants.PROJECTACTION);
		projectTagName = Activator.getDefault().getDefaultPreference(Constants.PROJECTTAGNAME);
		projectAttribute = Activator.getDefault().getDefaultPreference(Constants.PROJECTATTRIBUTE);
	}
	
	protected void getPreferredList(Object payload) {
		try {
			Document projectDoc = getResourceListFromServer(getBaseUrl()+projectAction);
			NodeList projectList = projectDoc.getElementsByTagName(projectTagName);
			for (int i = 0; i < projectList.getLength(); i++) {
				Element project = (Element)projectList.item(i);
				UtilRepository.updateProjectRepository(project.getTextContent().trim(), project.getAttribute(projectAttribute));
				projectCombo.add(project.getTextContent().trim());
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
	}
	
//	public static void main(String args[]) throws SAXException, IOException, ParserConfigurationException {
////		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
////		Document repoDoc = builder.parse("http://mpgbuild/codestriker/codestriker.pl?action=list_projects&format=xml");
////		NodeList repoList = repoDoc.getElementsByTagName("project");
////		for (int i = 0; i < repoList.getLength(); i++) {
////			Node repository = repoList.item(i);
////			System.out.println("prabhatk: "+repository.getTextContent());
//		
////		}
////		Projects p = new Projects();
//	}


	public void clear() {
		projectCombo.removeAll();
	}

}
